package com.xhs.controller;

import com.xhs.dto.response.ApiResponse;
import com.xhs.entity.Like;
import com.xhs.entity.User;
import com.xhs.security.UserPrincipal;
import com.xhs.service.LikeService;
import com.xhs.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.*;

/**
 * 点赞管理控制器
 */
@RestController
@RequestMapping("/contents/{contentId}/likes")
@Slf4j
public class LikeController {
    
    @Autowired
    private LikeService likeService;
    
    @Autowired
    private UserService userService;
    
    /**
     * 点赞或取消点赞
     */
    @PostMapping
    @PreAuthorize("hasRole('USER') or hasRole('ADMIN')")
    public ResponseEntity<ApiResponse<Boolean>> toggleLike(
            @PathVariable Long contentId,
            Authentication authentication) {
        try {
            UserPrincipal userPrincipal = (UserPrincipal) authentication.getPrincipal();
            User user = userService.findById(userPrincipal.getId())
                    .orElseThrow(() -> new RuntimeException("用户不存在"));
            
            boolean liked = likeService.toggleLike(contentId, user);
            String message = liked ? "点赞成功" : "取消点赞成功";
            
            log.info("用户 {} {} 内容 {}", user.getUsername(), message, contentId);
            return ResponseEntity.ok(ApiResponse.success(message, liked));
            
        } catch (Exception e) {
            log.error("点赞操作失败: {}", e.getMessage());
            return ResponseEntity.badRequest()
                    .body(ApiResponse.error("操作失败: " + e.getMessage()));
        }
    }
    
    /**
     * 检查是否已点赞
     */
    @GetMapping("/status")
    @PreAuthorize("hasRole('USER') or hasRole('ADMIN')")
    public ResponseEntity<ApiResponse<Boolean>> getLikeStatus(
            @PathVariable Long contentId,
            Authentication authentication) {
        try {
            UserPrincipal userPrincipal = (UserPrincipal) authentication.getPrincipal();
            User user = userService.findById(userPrincipal.getId())
                    .orElseThrow(() -> new RuntimeException("用户不存在"));
            
            boolean liked = likeService.isLiked(contentId, user);
            return ResponseEntity.ok(ApiResponse.success(liked));
            
        } catch (Exception e) {
            log.error("获取点赞状态失败: {}", e.getMessage());
            return ResponseEntity.badRequest()
                    .body(ApiResponse.error("获取状态失败: " + e.getMessage()));
        }
    }
    
    /**
     * 获取点赞数
     */
    @GetMapping("/count")
    public ResponseEntity<ApiResponse<Long>> getLikeCount(@PathVariable Long contentId) {
        try {
            long count = likeService.getLikeCount(contentId);
            return ResponseEntity.ok(ApiResponse.success(count));
            
        } catch (Exception e) {
            log.error("获取点赞数失败: {}", e.getMessage());
            return ResponseEntity.badRequest()
                    .body(ApiResponse.error("获取点赞数失败: " + e.getMessage()));
        }
    }
} 